package sqlinjection;

import core.User;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Formatter;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

/**
 *
 * @author Atuan
 */
@ManagedBean(name="userService")
@SessionScoped
public class UserService {
    
    //@ManagedProperty(value="new User()")
    private User currentUser = new User();
    private int maxAttempts = 3;
    private int attempts = 0;
    
    public void secureLogin(){
        try{
            if(attempts <= maxAttempts){
                if(DBHandler.secureLogin(currentUser.getLogin(), currentUser.getPassword())){ 
                    attempts = 0;
                    System.out.println("login succeeded");
                    currentUser = DBHandler.getUserData(currentUser.getLogin());
    //                currentUser.setSubscriber(1);
                    currentUser.setIsLoggedIn(true);
                    ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
                    ec.redirect(ec.getRequestContextPath() + "/search.jsf");
                } else {
                    attempts++;
                    FacesMessage message = new FacesMessage();
                    message.setSeverity(FacesMessage.SEVERITY_WARN);
                    message.setSummary("Login attempt failed! Username or password is incorrect.");
                    FacesContext.getCurrentInstance().addMessage("loginpropermess", message);
                }
            } else {
                FacesMessage message = new FacesMessage();
                message.setSeverity(FacesMessage.SEVERITY_ERROR);
                message.setSummary("Maximum number of attempts has been reached.");
                FacesContext.getCurrentInstance().addMessage("loginpropermess", message);
            }
        } catch(Exception e){
            System.out.println("login failed");            
        }
    }
    
    public boolean login(String login, String pass){
        currentUser.setLogin(login);
        currentUser.setPassword(pass);
        return login();
    }

    public boolean login(){
        try{
            if(DBHandler.login(currentUser.getLogin(), currentUser.getPassword())){ 
                System.out.println("login succeeded");
                currentUser = DBHandler.getUserData(currentUser.getLogin());
                currentUser.setIsLoggedIn(true);
                ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
                ec.redirect(ec.getRequestContextPath() + "/search.jsf");               
            } else {
                FacesMessage message = new FacesMessage();
                message.setSeverity(FacesMessage.SEVERITY_WARN);
                message.setSummary("Login attempt failed! Username or password is incorrect.");
                FacesContext.getCurrentInstance().addMessage("loginpropermess", message);
                return false;
            }
        } catch (Exception e){
            System.out.println("login failed");
        }
        return true;
    }
    
    public void logout()
    {
        try{
            
                currentUser.setIsLoggedIn(false);
                ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
                ec.redirect(ec.getRequestContextPath() + "/index.jsf");
        } catch (Exception e){
            System.out.println("login failed");
        }
    }
    
    public void redirect(String dest){
        try{
            ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
            ec.redirect(ec.getRequestContextPath() + "/search.jsf");
        } catch (Exception e){
            System.out.println("login failed");
        }
    }    
    
    public String easyAccessToEditMovie(){
        System.out.println("edit1");
        if(currentUser != null){
            currentUser.setSubscriber(1);
        System.out.println("edit2");
            return "editmovie.jsf";
        }
        return "";
    }
    
    public String easyAccesstoAddMovie(){
        if(currentUser != null){
            currentUser.setSubscriber(1);
            return "addmovie.jsf";
        }
        return "";
    }
    
    public  User getCurrentUser() {
        return currentUser;
    }

    public  void setCurrentUser(User currentUser) {
        this.currentUser = currentUser;
    }
}
